package com.canyou.common;

import cn.afterturn.easypoi.entity.ImageEntity;
import com.canyou.utils.WordUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * word导出Demo
 *
 * @author cd
 * @date 2019-07-03
 */
@Api(tags = "word导出Demo")
@RequestMapping(value = "/export")
@Slf4j
@RestController
public class ExportDemoController {

    @ApiOperation(value = "word导出demo")
    @GetMapping(value = "/export_word")
    public void export(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {
        String path = ResourceUtils.getURL("classpath:").getPath();
        Map<String, Object> params = createExportData();
        String temDir = path + "temp/";
        WordUtils.exportWord("template/export/simple.docx", temDir, "用户数据", params, response);
    }

    @ApiOperation(value = "pdf导出demo")
    @GetMapping(value = "/export_pdf")
    public void exportPdf(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {
        String path = ResourceUtils.getURL("classpath:").getPath();
        Map<String, Object> params = createExportData();
        String temDir = path + "temp/";
        WordUtils.exportPdf("template/export/simple.docx", temDir, "用户数据", params, response);
    }

    public Map<String, Object> createExportData() {
        Map<String, Object> params = new HashMap<>(3);
        params.put("department", "用户数据");
        ImageEntity image = new ImageEntity();
        image.setHeight(100);
        image.setWidth(100);
        image.setUrl("image/demo/bug.jpg");
        image.setType(ImageEntity.URL);
        params.put("img", image);
        List<Map<String, Object>> userList = new ArrayList<Map<String, Object>>();
        Map<String, Object> user = new HashMap<>(3);
        user.put("name", "test1");
        user.put("sex", "男");
        user.put("day", "1990-01-27");
        userList.add(user);
        userList.add(user);
        params.put("userList", userList);
        return params;
    }

}